package com.agile.leetcode.hutool.webserviceclient;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.webservice.SoapClient;
import org.w3c.dom.Document;

import javax.xml.xpath.XPathConstants;

/**
 * hutool用于soap协议，webservice的客户端
 * 服务端在mongodb的webservice模块中
 *
 * @Author:ChenZhangKun
 * @Date: 2021/9/17 14:30
 */
public class WebServiceClient {
    public static void main(String[] args) {
        //设置webservice服务地址
        SoapClient client = SoapClient.create("http://127.0.0.1:9999")
                // 设置要请求的方法，传入对应的命名空间
                .setMethod("getCarInfo", "http://service.czk.com/")
                // 设置参数和发布时指定的webParameter参数名称一致，值为自己设置
                .setParam("carBrand", "bmw");
        // 发送请求，参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
        String result = client.send(true);
        System.out.println(result);
        System.out.println("----------------------------");
        Document docResult = XmlUtil.readXML(result);
        Object brand = XmlUtil.getByXPath("//return//brand", docResult, XPathConstants.STRING);
        Object owner = XmlUtil.getByXPath("//return//owner", docResult, XPathConstants.STRING);
        Object plateNum = XmlUtil.getByXPath("//return//plateNum", docResult, XPathConstants.STRING);
        Object price = XmlUtil.getByXPath("//return//price", docResult, XPathConstants.STRING);
        System.out.println("brand = " + brand);
        System.out.println("owner = " + owner);
        System.out.println("plateNum = " + plateNum);
        System.out.println("price = " + price);
    }
}
